﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using GeneralDFS.BusinessLogical.Config;
using GeneralDFS.BusinessLogical.RemoteObject;
using GeneralDFS.DataEntity.Config;

namespace GeneralDFS.BusinessLogical.RemoteServers
{
     [Serializable]
    public class NameNodeRemoteServer
    {
        private IChannel _channel;
        private NameNodeConfig _nameNodeConfig;

        public void InitServer(NameNodeRemoteObject nameNodeRemoteObject)
        {
            _nameNodeConfig = nameNodeRemoteObject.GetNameNodeConfig();
            LogUtil.LogInfo("----------Starting init NameNodeRemote Server------------");
            TrackingServices.RegisterTrackingHandler(new RemoteTracking());//注册远程跟踪
            _channel = new TcpServerChannel(_nameNodeConfig.ServerName + "-Server", _nameNodeConfig.RpcPort);
            LogUtil.LogInfo(String.Format("channel name:{0} rpcPort:{1}", "namenode", _nameNodeConfig.RpcPort));
            ChannelServices.RegisterChannel(_channel,false);
            RemotingServices.Marshal(nameNodeRemoteObject, _nameNodeConfig.ServerName + "-server");
            LogUtil.LogInfo("----------init NameNodeRemote Server Success------------");
        }

        public void StopServer()
        {
            LogUtil.LogInfo("----------Stoping init NameNodeRemote Server------------");
            ((TcpServerChannel)_channel).StopListening(null);
            ChannelServices.UnregisterChannel(_channel);
            LogUtil.LogInfo("----------Stop NameNodeRemote Server Success------------");
        }
    }
}